package com.gxuwz.zlc.controller;

import com.gxuwz.zlc.common.Result;
import com.gxuwz.zlc.entity.Lab;
import com.gxuwz.zlc.entity.LabTechnician;
import com.gxuwz.zlc.entity.User;
import com.gxuwz.zlc.service.DepartmentService;
import com.gxuwz.zlc.service.LabTechnicianService;
import com.gxuwz.zlc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 实验室技术人员信息管理
 */
@Controller
@RequestMapping("labTechnician")
public class LabTechnicianController {

    @Autowired
    private LabTechnicianService labTechnicianService;

    // 用户表
    @Autowired
    private UserService userService;


    // 查询所有
    @RequestMapping("/findAll")
    @ResponseBody
    public List<LabTechnician> findLabTechnician(){
        List<LabTechnician> technicianList = labTechnicianService.findLabTechnician();
        return technicianList;
    }

    // 查询一个
    @GetMapping("/findOne/{techId}")
    @ResponseBody
    public Result get(@PathVariable String techId){
        LabTechnician labTechnicianById = labTechnicianService.findLabTechnicianById(techId);
        // code=0,msg=成功
        return Result.success(labTechnicianById);
    }

    /**
     * 管理员
     * 添加实验室技术人员，并在user表中添加信息
     * @param labTechnician
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public Map addLabTechnician(@RequestBody LabTechnician labTechnician){
        Map<String,String> map = new HashMap<>();
        try {
            // 把信息填加进实验室技术人员表
            labTechnicianService.addLabTechnician(labTechnician);
            // user表中添加
            User user = new User();
            user.setUsername(labTechnician.getTechId());
            user.setPassword(labTechnician.getPassword());
            user.setPerms("technician");
            userService.addUser(user);

            map.put("code","200");
            map.put("msg","修改成功");
        } catch (Exception e) {
            map.put("code","500");
            map.put("msg","添加失败");
            e.printStackTrace();
        }
        return map;
    }

    // 修改
    @RequestMapping("/update")
    @ResponseBody
    public Map updateLabTechnician(@RequestBody LabTechnician labTechnician){
        labTechnicianService.updateLabTechnician(labTechnician);
        Map<String,String> map = new HashMap<>();
        map.put("code","200");
        map.put("msg","修改成功");
        return map;
    }

    // 删除
    @RequestMapping("/delete")
    @ResponseBody
    public Map deleteLabTechnician(@RequestParam(name = "techId") String techId){
        labTechnicianService.deleteLabTechnician(techId);
        Map<String,String> map = new HashMap<>();
        map.put("code","200");
        map.put("mag","删除成功");
        return map;
    }

}
